package com.example.zyfypt_206shj.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

import com.example.zyfypt_206shj.R;
import com.example.zyfypt_206shj.bean.SessionID;
import com.example.zyfypt_206shj.iface.GuanzhuListener;
import com.example.zyfypt_206shj.iface.IsGuanzhuListener;
import com.example.zyfypt_206shj.iface.IsShoucangListener;
import com.example.zyfypt_206shj.iface.ShoucangListener;
import com.example.zyfypt_206shj.iface.unGuanzhuListener;
import com.example.zyfypt_206shj.iface.unShoucangListener;
import com.example.zyfypt_206shj.model.GuanzhuModel;
import com.example.zyfypt_206shj.model.IsGuanzhuModel;
import com.example.zyfypt_206shj.model.IsShoucangModel;
import com.example.zyfypt_206shj.model.ShoucangModel;
import com.example.zyfypt_206shj.model.unGuanzhuModel;
import com.example.zyfypt_206shj.model.unShoucangModel;

public class ViewVideoActivity extends AppCompatActivity {
    private VideoView videoView;
    private String path="";
    private String BASEURL="http://amicool.neusoft.edu.cn/";
    private ProgressDialog dialog;
    private boolean isShoucang;
    private boolean isGuanzhu;
    private IsGuanzhuModel isGuanzhuModel;
    private IsShoucangModel isShoucangModel;
    private ShoucangModel shoucangModel;
    private GuanzhuModel guanzhuModel;
    private unShoucangModel unshoucangModel;
    private unGuanzhuModel unguanzhuModel;
    private static MenuItem menu_shoucang = null,menu_guanzhu = null;
    private Intent intent_uper;

    private IsGuanzhuListener isGuanzhuListener = new IsGuanzhuListener() {
        @Override
        public void onResponse(String result) {
            if(result.equals("1")){
//                menu_guanzhu.setTitle("未关注");
                isGuanzhu = false;
            }else if(result.equals("0")){
//                menu_guanzhu.setTitle("已关注");
                isGuanzhu = true;
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(ViewVideoActivity.this, "fail:", Toast.LENGTH_SHORT).show();
        }
    };

    private IsShoucangListener isShoucangListener = new IsShoucangListener() {
        @Override
        public void onResponse(String result) {
            Toast.makeText(ViewVideoActivity.this, "" + result, Toast.LENGTH_SHORT).show();
            if(result.equals("1")){
//                menu_shoucang.setTitle("未收藏");
                isShoucang = false;
            }else if(result.equals("0")){
//                menu_shoucang.setTitle("已收藏");
                isShoucang = true;
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(ViewVideoActivity.this, "fail:" + msg, Toast.LENGTH_SHORT).show();
        }
    };

    private ShoucangListener shoucangListener = new ShoucangListener() {
        @Override
        public void onResponse(String result) {
            if(result.equals("0")){
                Toast.makeText(ViewVideoActivity.this, "收藏失败", Toast.LENGTH_SHORT).show();
            }else{
//                menu_shoucang.setTitle("已收藏");
                isShoucang = true;
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(ViewVideoActivity.this, "fail:" + msg, Toast.LENGTH_SHORT).show();
        }
    };

    private unShoucangListener unshoucangListener = new unShoucangListener() {
        @Override
        public void onResponse(String result) {
            if(result.equals("1")){
                Toast.makeText(ViewVideoActivity.this, "取消收藏成功", Toast.LENGTH_SHORT).show();
//                menu_shoucang.setTitle("未收藏");
                isShoucang = false;
            }else if(result.equals("0")){
                Toast.makeText(ViewVideoActivity.this, "取消收藏失败", Toast.LENGTH_SHORT).show();
//                menu_shoucang.setTitle("已收藏");
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(ViewVideoActivity.this, "fail:" + msg, Toast.LENGTH_SHORT).show();
        }
    };

    private GuanzhuListener guanzhuListener = new GuanzhuListener() {
        @Override
        public void onResponse(String result) {
            if(result.equals("0")){
                Toast.makeText(ViewVideoActivity.this, "关注失败", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(ViewVideoActivity.this, "关注成功", Toast.LENGTH_SHORT).show();
//                menu_guanzhu.setTitle("已关注");
                isGuanzhu = true;
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(ViewVideoActivity.this, "fail:" + msg, Toast.LENGTH_SHORT).show();
        }
    };

    private unGuanzhuListener unguanzhuListener = new unGuanzhuListener() {
        @Override
        public void onResponse(String result) {
            if(result.equals("1")){
                Toast.makeText(ViewVideoActivity.this, "取消关注成功", Toast.LENGTH_SHORT).show();
//                menu_guanzhu.setTitle("未关注");
                isGuanzhu = false;
            }else if(result.equals("0")){
                Toast.makeText(ViewVideoActivity.this, "取消关注失败", Toast.LENGTH_SHORT).show();
//                menu_guanzhu.setTitle("已关注");
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(ViewVideoActivity.this, "fail:" + msg, Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_video);

        intent_uper = getIntent();
        //判断是否收藏关注


        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//设置屏幕方向为横向
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏
        getWindow().setFormat(PixelFormat.TRANSLUCENT);//透明
        videoView=(VideoView)findViewById(R.id.videoView);

        path=BASEURL+"Uploads/video/video/"+getIntent().getStringExtra("videopath");//获取视频全路径
        dialog=ProgressDialog.show(ViewVideoActivity.this, "视频加载中...", "请您稍候");//进度条

        Uri uri = Uri.parse(path);
        videoView.setMediaController(new MediaController(this));//媒体播放控制工具
        videoView.setVideoURI(uri);//设置视频路径
        videoView.setOnPreparedListener(new MyPlayerOnPreparedListener());//播放开始回调
        videoView.setOnCompletionListener( new MyPlayerOnCompletionListener());//播放完成回调
        videoView.requestFocus();// 让VideoView获取焦点
        videoView.start();//开始播放

    }

    //自定义子类，监听视频准备好，消除加载对话框
    class MyPlayerOnPreparedListener implements MediaPlayer.OnPreparedListener {
        @Override
        public void onPrepared(MediaPlayer mp) {
            videoView.setBackgroundColor(Color.argb(0, 0, 255, 0));
            dialog.dismiss();
        }
    }
    //自定义子类，监听播放完成，显示完成
    class MyPlayerOnCompletionListener implements MediaPlayer.OnCompletionListener {

        @Override
        public void onCompletion(MediaPlayer mp) {
            Toast.makeText( ViewVideoActivity.this, "播放完成", Toast.LENGTH_SHORT).show();
            //getSupportActionBar().show();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
//        getMenuInflater().inflate(R.menu.content_setting, menu);
        menu_guanzhu = menu.add(Menu.NONE,1,1,"关注");
        menu_guanzhu.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

        menu_shoucang = menu.add(Menu.NONE,2,2,"收藏");
        menu_shoucang.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

        isGuanzhuModel = new IsGuanzhuModel(this);
        isGuanzhuModel.getResult("userfocus", Integer.parseInt(intent_uper.getStringExtra("userid")), isGuanzhuListener);
        isShoucangModel = new IsShoucangModel();
        isShoucangModel.getResult(intent_uper.getStringExtra("mod"), intent_uper.getIntExtra("id",0), isShoucangListener);

        new Thread(){
            @Override
            public void run() {
                if(isShoucang){
                    menu_shoucang.setTitle("已收藏");
                }else{
                    menu_shoucang.setTitle("未收藏");
                }

                if(isGuanzhu){
                    menu_guanzhu.setTitle("已关注");
                }else {
                    menu_guanzhu.setTitle("未关注");
                }
                try {
                    sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case 1:
                if(isGuanzhu){
                    //正在关注，这里需要启动取消关注模块
                    unguanzhuModel = new unGuanzhuModel();
                    unguanzhuModel.getResult("userfocus", Integer.parseInt(intent_uper.getStringExtra("userid")), SessionID.sessionID, unguanzhuListener);
                    menu_guanzhu.setTitle("未关注");
                }else {
                    guanzhuModel = new GuanzhuModel(this);
                    guanzhuModel.getResult("userfocus", Integer.parseInt(intent_uper.getStringExtra("userid")), SessionID.sessionID, guanzhuListener);
                    menu_guanzhu.setTitle("已关注");
                }
//                Toast.makeText(this, "您已关注", Toast.LENGTH_SHORT).show();
                break;
            case 2:
                if(isShoucang){
                    //正在收藏，这里需要启动取消收藏模块
                    unshoucangModel = new unShoucangModel();
                    unshoucangModel.getResult(intent_uper.getStringExtra("mod"), intent_uper.getIntExtra("id",0), SessionID.sessionID, unshoucangListener);
                    menu_shoucang.setTitle("未收藏");
                }else {
                    shoucangModel = new ShoucangModel();
                    shoucangModel.getResult(intent_uper.getStringExtra("mod"), intent_uper.getIntExtra("id",0), SessionID.sessionID, shoucangListener);
                    menu_shoucang.setTitle("已收藏");
                }
//                Toast.makeText(this, "您已收藏", Toast.LENGTH_SHORT).show();
                break;
            default:
        }
        return true;
    }
}
